# from tkinter import Tk, Listbox, PhotoImage
from PIL import Image, ImageTk

# def add_image_to_listbox(lstbox, image_path):
#     # 加载图片
#     image = Image.open(image_path)
#     photo = ImageTk.PhotoImage(image)
#     # 添加图片到 Listbox
#     lstbox.insert('end', photo)

# # 创建主窗口
# root = Tk()
# root.title("图片 Listbox")

# # 创建 Listbox
# lstbox = Listbox(root)
# lstbox.pack()

# # 添加图片
# add_image_to_listbox(lstbox, '/Users/liuhaoyu/Desktop/2023/WechatIMG10616.png')
# add_image_to_listbox(lstbox, '/Users/liuhaoyu/Desktop/2023/WechatIMG10616.png')
# add_image_to_listbox(lstbox, '/Users/liuhaoyu/Desktop/2023/WechatIMG10616.png')

# # 主循环
# root.mainloop()




import tkinter as tk 
from tkinter import Label, ttk,PhotoImage
import random

def generate_random_color():
    # 生成随机的十六进制颜色值
    color = "#{:06x}".format(random.randint(0, 16777215))
    return color

random_color = generate_random_color()
print(random_color)

root = tk.Tk()
grid_frame = ttk.Frame(root)
grid_frame.pack()
 

root.geometry("800x600+500+500")

# 定义每行或列的数量
rows = 3
cols = 3
 

# image_file = "/Users/liuhaoyu/Desktop/2023/WechatIMG10616.png"
# image = PhotoImage(file=image_file)
#         # 创建标签并显示图片
# label = Label(root, image=image)
# label.pack()

x = 0
for i in range(rows):
    for j in range(cols):
        
        # 创建标签并放置到对应位置上
        label2 = ttk.Label(grid_frame, text=f"({i}, {j})")
        label2.grid(row=i, column=j)
        # image_path = '/Users/liuhaoyu/Desktop/2023/WechatIMG10616.png'
        # image = Image.open(image_path)
        # photo = ImageTk.PhotoImage(image)
        image_file = f"/Users/liuhaoyu/Desktop/2023/{x}.png"
        image = Image.open(image_file)
        # 设置新的大小
        new_width = 100
        new_height = 100
        image = image.resize((new_width, new_height))

        image = PhotoImage(file=image_file)
        random_color = generate_random_color()
        # 创建标签并显示图片
        label = Label(grid_frame, fg='red',bg=random_color,image=image,width=100, height=100,text=f"({i}, {j})")
        # label.pack()
        label.grid(row=i, column=j)
        x = x + 1

        
root.mainloop()